package com.gexiaobao.pigeon.viewmodel

import androidx.lifecycle.MutableLiveData
import com.gexiaobao.pigeon.app.model.bean.OrgListResponse
import com.gexiaobao.pigeon.app.model.bean.RaceInfoResponse
import com.gexiaobao.pigeon.app.network.ListDataUiState
import com.gexiaobao.pigeon.app.network.apiService
import com.gexiaobao.pigeon.app.util.RxToast
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.databind.StringObservableField
import me.hgj.jetpackmvvm.ext.request

/**
 * created by : huxiaowei
 * @date : 20221114
 * Describe :
 */
class OrganizationViewModel : BaseViewModel() {
    private var pageNo: Int = 1
    private val pageSize: Int = 20

    var searchResult = StringObservableField()
    var searchname = StringObservableField()


//    /** 获取赛事信息 */
//    val raceInfoResult: MutableLiveData<RaceInfoResponse> = MutableLiveData()
//    fun getRaceInfo(raceId: Int, orgId: Int) {
//        request({ apiService.getRaceInfo(raceId, orgId) }, {
//            raceInfoResult.value = it
//        }, {
//            RxToast.warning(it.msg)
//        })
//    }

    //搜索组织
    var searchOrgData: MutableLiveData<ListDataUiState<OrgListResponse>> = MutableLiveData()

    fun getOrganizationCallBack(isRefresh: Boolean, searchName: String, mStatus: Int) {
        if (isRefresh) {
            pageNo = 1
        }
        val body = hashMapOf<String, Any>().apply {
            put("name", searchName)
            put("ps", pageSize)
            put("pn", pageNo)
            put("status", mStatus)
        }
        request({ apiService.getSearchOrgList(body) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                total = it.total,
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.data
            )
            searchOrgData.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false,
                errMessage = it.msg,
                isRefresh = isRefresh,
                listData = arrayListOf<OrgListResponse>()
            )
            RxToast.showToast(it.msg)
            searchOrgData.value = listDataUiState
        })
    }

    fun getOrgJoinList(isRefresh: Boolean, type: String, status: Int) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getOrgJoinList(type, status) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.data
            )
            searchOrgData.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false,
                errMessage = it.msg,
                isRefresh = isRefresh,
                listData = arrayListOf<OrgListResponse>()
            )
            RxToast.showToast(it.msg)
            searchOrgData.value = listDataUiState
        })
    }

}